package com.hdyg.sixg.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.bumptech.glide.Glide;
import com.hdyg.sixg.BuildConfig;
import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.AppManager;
import com.hdyg.sixg.activity.mine.AddressActivity;
import com.hdyg.sixg.activity.mine.AppealActivity;
import com.hdyg.sixg.activity.mine.CompleteInfoActivity;
import com.hdyg.sixg.activity.mine.MyTeamActivity;
import com.hdyg.sixg.activity.mine.NoticeActivity;
import com.hdyg.sixg.activity.mine.PhoneActivity;
import com.hdyg.sixg.activity.mine.RealNameActivity;
import com.hdyg.sixg.activity.mine.ShareActivity;
import com.hdyg.sixg.activity.mine.UpdatePwdActivity;
import com.hdyg.sixg.entry.MineBean;
import com.hdyg.sixg.mvp.Contrant.CFragMine;
import com.hdyg.sixg.mvp.base.BasePresenter;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.base.SpMsg;
import com.hdyg.sixg.mvp.presenter.PFragMine;
import com.hdyg.sixg.util.ImageLoadUtil;
import com.hdyg.sixg.util.SPUtils;
import com.hdyg.sixg.util.StringUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.uuzuche.lib_zxing.decoding.FinishListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import static android.app.Activity.RESULT_OK;

/**
 * @author CZB
 * @describe 我的
 * @time 2019/4/4 10:20
 */
public class MineFragment extends BaseFragment implements CFragMine.IVFragMine {

    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.sv_user_info)
    SuperTextView svUserInfo;
    @BindView(R.id.sv_all_money)
    SuperTextView svAllMoney;
    @BindView(R.id.sv_current_money)
    SuperTextView svCurrentMoney;
    @BindView(R.id.sv_phone)
    SuperTextView svPhone;
    @BindView(R.id.sv_info)
    SuperTextView svInfo;
    @BindView(R.id.sv_team)
    SuperTextView svTeam;
    @BindView(R.id.sv_pwd)
    SuperTextView svPwd;
    @BindView(R.id.sv_address)
    SuperTextView svAddress;
    @BindView(R.id.sv_share)
    SuperTextView svShare;
    @BindView(R.id.sv_realname)
    SuperTextView svRealname;
    @BindView(R.id.sv_appeal)
    SuperTextView svAppeal;
    @BindView(R.id.sv_notice)
    SuperTextView svNotice;
    @BindView(R.id.sv_version)
    SuperTextView svVersion;
    @BindView(R.id.bt_out)
    Button btOut;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;

    private final int REQUEST_CODE = 0x000001;
    private CFragMine.IPFragMine mPresenter;
    private MineBean dataBean;

    @Override
    protected void createPresenter() {
        mPresenter = new PFragMine(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView() {
        initListener();
        llTopLeft.setVisibility(View.GONE);
        tvTopTitle.setText(R.string.mine_title);
        String VERSION_NAME = "V " + BuildConfig.VERSION_NAME;
        svVersion.setRightString(VERSION_NAME).setRightTextColor(mContext.getResources().getColor(R.color.white));
    }

    @Override
    protected void initData() {
        mPresenter.pGetMineInit(BaseUrl.MY_INDEX_URL, GetParamUtil.getMineParam());
    }

    private void initListener() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            initData();
        });
        //总钱包
        svAllMoney.setOnSuperTextViewClickListener(superTextView -> {

        });
        //流通钱包
        svCurrentMoney.setOnSuperTextViewClickListener(superTextView -> {

        });
        //复制
        svUserInfo.setRightBottomTvClickListener(() -> {
            StringUtil.copyTextMethod(mContext,dataBean.getWallet_token());
        });
        //手机号码
        svPhone.setOnSuperTextViewClickListener(superTextView -> {
            mBundle = new Bundle();
            mBundle.putInt(SpMsg.INTENT_CODE, PhoneActivity.GET_CODE_TYPE);
            mBundle.putString(SpMsg.USERNAME, dataBean.getUsername());
            mBundle.putString(SpMsg.USER_PHONE, dataBean.getPhone());
            startActivityForResult(PhoneActivity.class, mBundle, REQUEST_CODE);
        });
        //完善资料
        svInfo.setOnSuperTextViewClickListener(superTextView -> {
            startActivity(CompleteInfoActivity.class);
        });
        //我的团队
        svTeam.setOnSuperTextViewClickListener(superTextView -> {
            startActivity(MyTeamActivity.class);
        });
        //修改密码
        svPwd.setOnSuperTextViewClickListener(superTextView -> {
            mBundle = new Bundle();
            mBundle.putString(SpMsg.USER_PHONE, dataBean.getPhone());
            startActivity(UpdatePwdActivity.class, mBundle);
        });
        //收货地址
        svAddress.setOnSuperTextViewClickListener(superTextView -> {
            mBundle = new Bundle();
            mBundle.putInt(SpMsg.INTENT_CODE, AddressActivity.ADDRESS_EDIT);
            startActivity(AddressActivity.class, mBundle);
        });
        //分享推荐
        svShare.setOnSuperTextViewClickListener(superTextView -> {
            startActivity(ShareActivity.class);
        });
        //实名认证
        svRealname.setOnSuperTextViewClickListener(superTextView -> {
            startActivity(RealNameActivity.class);
        });
        //我的申诉
        svAppeal.setOnSuperTextViewClickListener(superTextView -> {
            startActivity(AppealActivity.class);
        });
        //系统公告
        svNotice.setOnSuperTextViewClickListener(superTextView -> {
            startActivity(NoticeActivity.class);
        });
//        svVersion.setOnSuperTextViewClickListener(superTextView -> {
//            toastNotifyShort("版本号");
//        });
        btOut.setOnClickListener(v -> {
            SPUtils.put(SpMsg.USERNAME,"");
            SPUtils.put(SpMsg.USERPASSWORD,"");
            AppManager.getAppManager().staLoginActivity();
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case REQUEST_CODE:
                initData();
                break;
        }
    }

    @Override
    public void vGetMineInitSuccess(MineBean dataBean) {
        this.dataBean = dataBean;
        String username = String.format(getResources().getString(R.string.mine_name), dataBean.getUsername());
        String phone = String.format(getResources().getString(R.string.mine_phone), dataBean.getPhone());
        String walletToken = StringUtil.cutLentgthReplace(dataBean.getWallet_token(),22,"...");
        svUserInfo.setLeftTopString(username)
                .setRightTopString(dataBean.getUser_level())
                .setLeftString(phone)
                .setLeftBottomString(walletToken)
                .setRightBottomString(getResources().getString(R.string.recharge_copy));
        svAllMoney.setCenterTopString(dataBean.getAll_wallet());
        svCurrentMoney.setCenterTopString(dataBean.getCare_wa());
    }
}
